Component({
  properties: {
    target: {
      type: Number,
      observer(n, o) {
        this.setData({
          leftTime: n,
        });
      },
    },
  },
  data: {
    day: [], //天
    hour: [], //时
    minutes: [], //分
    seconds: [], //秒
    result: "", //自定义格式返回页面显示结果
    timer: null,
    leftTime: 0, //倒计时的时间错
  },
  lifetimes: {
    attached() {
      this.tick();
    },
    detached() {
      clearTimeout(this.data.timer);
    },
  },
  methods: {
    tick: function() {
      const dy = Math.floor(this.data.leftTime / 60 / 60 / 24);
      const hr = Math.floor((this.data.leftTime / 3600) % 24);
      const min = Math.floor((this.data.leftTime / 60) % 60);
      const sec = Math.floor(this.data.leftTime % 60);
      this.setData({
        day: dy < 10 ? ["0", dy] : String(dy).split(''),
        hour: hr < 10 ? ["0", hr] : String(hr).split(''),
        minutes: min < 10 ? ["0", min] : String(min).split(''),
        seconds: sec < 10 ? ["0", sec] : String(sec).split(''),
      });

      this.data.timer = setTimeout(() => {
        if (this.data.leftTime <= 0) {
          this.onEnd();
          return;
        }
        this.data.leftTime -= 1;
        this.tick();
      }, 1000);
    },
    onEnd: function() {
      this.triggerEvent("onEnd");
    },
    isNotZeroSection: function(arr) {
      console.log(arr)
      return !(arr[0] === '0' && arr[1] === '0');
    },
  },
});